<?php

namespace App\Http\Controllers\Admin;

use App\Http\Resources\Admin\Withdraw as WithdrawResource;
use App\Http\Resources\Admin\WithdrawCollection;
use App\Http\Controllers\ApiController;
use App\Models\ShopBalanceLog;
use Illuminate\Http\Request;
use App\Models\Withdraw;
use App\Models\Shop;
use Auth;
use DB;

class WithdrawController extends ApiController
{
    /**
     * 提现列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $limit = $request->input('size');
        $date = $request->input('date');
        $invoice = $request->input('invoice');
        $status = $request->input('status');

        $orm = Withdraw::orderBy('created_at', 'DESC');

        $status && $orm->where('status', $status);
        $invoice && $orm->where('invoice', $invoice);

        if($date) {
            if($date[0] && time($date[0])) {
                $orm->where('created_at', '>', $date[0]);
            }

            if($date[1] && time($date[1])) {
                $orm->where('created_at', '<', $date[1]);
            }
        }

        $orm->with(['shop' => function($query) {
            $query->select('id', 'name');
        }]);

        return (new WithdrawCollection($orm->paginate($limit)));

    }
}
